#include "Entity.h"

Entity::Entity()
{
	Position = sf::Vector2f(0,0);
	Velocity = sf::Vector2f(0,0);
	Angle = 0.f;
	Rotation = 0.f;

	MainSprite = NULL;
	MainTexture = NULL;
	MainWindow = NULL;

}

Entity::Entity(sf::Vector2f nPosition, sf::Vector2f nVelocity, float nAngle, float nRotation, std::string TexFileName, sf::RenderWindow *nWindow)
{
	MainWindow = nWindow;
	MainSprite = NULL;
	MainTexture = NULL;

	Position = nPosition;
	Velocity = nVelocity;
	Angle = nAngle;
	Rotation = nRotation;

	if(!LoadTexture(TexFileName))
	{
		if(MainSprite != NULL)
		{
			delete MainSprite;
			MainSprite = NULL;
		}

		if(MainTexture != NULL)
		{
			delete MainTexture;
			MainTexture = NULL;
		}
	}
}

Entity::Entity(sf::Vector2f nPosition, sf::Vector2f nVelocity, float nAngle, float nRotation, sf::Texture* EntityTexture, sf::RenderWindow *nWindow)
{
	sf::Rect<int> TextureRect;
	MainWindow = nWindow;
	MainSprite = NULL;
	MainTexture = NULL;

	Position = nPosition;
	Velocity = nVelocity;
	Angle = nAngle;
	Rotation = nRotation;

	MainTexture = EntityTexture;
	if(MainTexture)
	{
		MainSprite = new sf::Sprite(*MainTexture);
		TextureRect = MainSprite->getTextureRect();
		MainSprite->setOrigin(TextureRect.width/2.f, TextureRect.height/2.f);
	}
}

Entity::~Entity()
{
	if(MainSprite != NULL)
	{
		delete MainSprite;
		MainSprite = NULL;
	}
}

bool Entity::LoadTexture( std::string FileName )
{
	sf::Rect<int> TextureRect;

	if(MainTexture != NULL)
	{
		delete MainTexture;
		MainTexture = NULL;
	}

	if(MainSprite != NULL)
	{
		delete MainSprite;
		MainSprite = NULL;
	}

	MainTexture = new sf::Texture();
	if(!MainTexture->loadFromFile(FileName))
		return false;

	MainSprite = new sf::Sprite(*MainTexture);

	TextureRect = MainSprite->getTextureRect();

	MainSprite->setOrigin(TextureRect.width/2.f, TextureRect.height/2.f);

	MainSprite->setPosition(Position);

	return true;
}

void Entity::Update( float dt)
{
	if(MainSprite)
	{
		Position += Velocity * dt;
		Angle += Rotation * dt;
		MainSprite->setPosition(Position);
		MainSprite->setRotation(Angle);
	}
}

void Entity::Render( void )
{

	if(MainWindow && MainSprite)
	{
		MainWindow->draw(*MainSprite);	
	}
}

void Entity::SetAngle(float nAngle)
{
	Angle = nAngle;
}

void Entity::AddToAngle(float AddMe)
{
	Angle += AddMe;
}

void Entity::SetRotationalSpeed(float nRotation)
{
	Rotation = nRotation;
}

void Entity::AddToRotationalSpeed(float nRotation)
{
	Rotation += nRotation;
}

void Entity::SetPosition(float nX, float nY)
{
	Position = sf::Vector2f(nX, nY);
}

void Entity::SetPosition(sf::Vector2f nPosition)
{
	Position = nPosition;
}

void Entity::AddToPosition(float AddX, float AddY)
{
	Position += sf::Vector2f(AddX, AddY);
}

void Entity::AddToPosition(sf::Vector2f AddMe)
{
	Position += AddMe;
}

void Entity::SetVelocity(float nX, float nY)
{
	Velocity = sf::Vector2f(nX, nY);
}

void Entity::SetVelocity(sf::Vector2f nVelocity)
{
	Velocity = nVelocity;
}

void Entity::AddToVelocity(float nX, float nY)
{
	Velocity += sf::Vector2f(nX, nY);
}

void Entity::AddToVelocity(sf::Vector2f AddMe)
{
	Velocity += AddMe;
}

sf::Vector2f Entity::GetPosition( void )
{
	return Position;
}

sf::Vector2f Entity::GetVelocity( void )
{
	return Velocity;
}

sf::Sprite* Entity::GetSprite( void )
{
	return MainSprite;
}

sf::Texture* Entity::GetTexture( void )
{
	return MainTexture;
}

float Entity::GetAngle( void )
{
	return Angle;
}

float Entity::GetRotation( void )
{
	return Rotation;
}

sf::RenderWindow* Entity::GetRenderWindow( void )
{
	return MainWindow;
}